<!doctype html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=0, width=device-width"/>
    <meta name="format-detection" content="telephone=no" />
    <meta name="format-detection" content="address=no" />
    <title>api</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css" />
    <link rel="stylesheet" type="text/css" href="../css/common.css" />
    <link rel="stylesheet" type="text/css" href="../css/userLogin.css" />
</head>

<body>
    <div id="wrap">
        
        <div id="main">
        
        		<div class="login-logo">
        			<img src="../image/app-logo.png" />
        		</div>
        
        		<div class="login-input-wrap">
                <input type="number" placeholder="输入手机号"  class="phonenum-input" id="phonenum" value="" />
            </div>
        
        		<div class="sms-input-wrap">
                <input type="number" placeholder="输入验证码"  class="sms-input" id="code" value="" />
                <span onclick="sendSms(this)" class="btn-send-sms">获取验证码</span>
            </div>
            
            <div class="login-input-wrap">
                <input type="password" placeholder="输入不少于6位的密码"  class="login-input" id="password" value="" />
            </div>
            
            <div onclick="registerSubmit()" class="login-btn register-btn">注册</div>
        
        </div>
        
    </div>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/common.js"></script>
<script>
	var smsVerify = null;
	
	apiready = function () {
		smsVerify = api.require('smsVerify');
			
		// 初始化
		register();
	}
	// 注册，初始化
	function register() {
		smsVerify.register(function(ret, err) {
			if (ret.status) {
				// TODO
			} else {
				api.alert({
					msg : err.code + ' 注册失败'
				});
			}
		});
	}
	
	function sendSms(ele) {
		var phonenum = $api.byId('phonenum').value;
		if (phonenum != null && phonenum.length == 11) {
		
			var password = $api.byId('password').value;
			if (password == null) {
				api.alert({
					msg : '密码为空或两次密码不一致'
				}, function(ret, err) {
					return;
				});
				return;
			}
			
			time(ele);
			
			smsVerify.sms({
				phone : phonenum,
			}, function(ret, err) {
				if (ret.status) {
					api.toast({
						msg : '短信发送成功'
					});
				} else {
					api.toast({
						msg : JSON.stringify(err)
					});
				}
			});
		} else {
			api.toast({
	            msg:'手机号输入有误！'
            });
		}	
	}
	
	var wait = 60;
	function time(o) {
		if (wait == 0) {
			o.onclick = function() {
				sendSms(o);
			}
			o.innerHTML = "获取验证码";
			wait = 60;
		} else {
			o.onclick = function() {
				return false;
			};
			o.innerHTML = "重新发送(" + wait + ")";
			wait--;
			setTimeout(function() {
				time(o)
			}, 1000)
		}
	}
	
	// 注册
	function registerSubmit() {
		
		api.openWin({
					                name: 'register-edit',
        								url: './register-edit/register-edit.html',
        								opaque: true,
        								vScrollBarEnabled: false
				                });    
				                return false;
				                
		
		var phonenum = $api.byId("phonenum").value;
		var password = $api.byId('password').value;
		
		var code = $api.byId("code").value;
		if (code.length == 0) {
			api.toast({
	            msg:'请输入验证码'
            });
            
            return false;
		}
		
		
		if (phonenum.length == 11 && password.length >= 6) {
		
//			smsVerify.verify({
//				phone : phonenum,
//				code : code,
//			}, function(ret, err) {
//				if (ret.status) {
					
					// TODO
					api.showProgress({
       					title: '正在注册...',
        					modal: false
    					});				
					var register_url = "http://dagudunqianduan.com/auth/register";
					
					var params = {
						phone: phonenum,
						password: password
					}
					
					api.ajax({
	                    url: register_url,
	                    method: 'post',
	                    cache: false,
	                    timeout: 20,
					    headers: {
				        		"Content-type" : "application/json;charset=UTF-8",
				        		"Accept": "application/json",
				        },
				        data: {
				        		body: JSON.stringify(params)
				        }
	                    
                    },function(ret,err){
						
						api.hideProgress();
						
						if (ret.accessToken) {
							
							storeAccessToken(ret.accessToken);
							
							api.toast({
	                            msg:'注册成功！'
                            });
                            
                            setTimeout(function () {
				                
				                api.openWin({
					                name: 'register-edit',
        								url: './register-edit/register-edit.html',
        								opaque: true,
        								vScrollBarEnabled: false
				                });
				                	
				            }, 100);
				
						} else {
						
							api.toast({
								msg : err.msg
							});
						
						}
						
                    });
					
					

//				} else {
//					api.toast({
//	           			msg:'验证失败，请查看验证码是否正确！'
//          			});
//				}
//			});
			
		} else {
			api.toast({
				msg : '手机号或密码格式不正确'
			});
		}
	}
	
</script>

</body>
</html>

